/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-04-15 17:26:25
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-14 09:14:39
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/new_air_cooling_page/component/stastics_info_widget.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/new_air_cooling_page/component/dynamic_tabview.dart';
import 'package:yt_devops/services/predicates.dart';

class StasticsInfoModel {
  final String dataKey;
  final String dataName;
  final String? unitName;
  final Map? dataType;
  const StasticsInfoModel(
      {required this.dataKey,
      required this.dataName,
      this.unitName,
      this.dataType});
}

class StasticsInfoWidget extends StatefulWidget {
  final int deviceId;
  final String apiUrl;
  final List<StasticsInfoModel> modelList;
  const StasticsInfoWidget(
      {required this.deviceId,
      required this.apiUrl,
      required this.modelList,
      super.key});

  @override
  State<StasticsInfoWidget> createState() => _StasticsInfoWidgetState();
}

class _StasticsInfoWidgetState extends State<StasticsInfoWidget> {
  List list = [];

  @override
  void initState() {
    loadData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DynamicTabView(
      list,
      isCanScroll: false,
      maxWidth: ScreenUtil().screenWidth - 24.w,
    );
  }

  loadData() => Configs.httpClient.requestWithLoading(() async {
        var data = await Configs.httpClient.get('$preApi/${widget.apiUrl}',
            queryParameters: {'deviceId': widget.deviceId});

        list = handleData(data);
        setState(() {});
      }, showLoading: false);

  List handleData(Map data) => widget.modelList
      .map((e) => {
            'name': e.dataName,
            'id': e.dataKey,
            'value': data[e.dataKey],
            'dataType': e.dataType ??
                {
                  'type': 'double',
                  'specs': {"unit": e.unitName ?? ''}
                }
          })
      .toList();
}
